/**
 * Created by wxbing (wxbing000@163.com) on 2021/7/27.
 */
const fs = require("fs");
const parser = new (require('xmldom')).DOMParser();
const serializer = new (require('xmldom')).XMLSerializer;

fs.readFile('./sbc.xml', function (err, data) {
    const xmlString = data.toString();
    // console.log(xmlString);
    console.log('============================================')
    const doc = parser.parseFromString(xmlString);
    // doc.documentElement.setAttribute('x', 'y')
    // doc.documentElement.setAttributeNS('./lite', 'c:x', 'y2')
    // console.info(doc)

    const nodes = doc.documentElement.childNodes;
    for (let index = 0, size = nodes.length; index < size; index++) {
        const node = nodes.item(index);
        // TEXT_NODE
        if (3 === node.nodeType) {
            continue;

            // ELEMENT_NODE
        } else if (1 === node.nodeType) {
            if (node.nodeName === 'webCallAddress') {
                let element = doc.createElement('address');
                element.textContent = '192.168.2.24100';
                node.appendChild(element)
            } else if (node.nodeName === 'ipAddress') {
                // node.nodeValue = '123123123'
                node.textContent = 'afasfasdf'
            }

            // COMMENT_NODE
        } else if (8 === node.nodeType) {
            // console.log(node.nodeValue)
        }
    }

    console.log(serializer.serializeToString(doc))

    fs.writeFile(
        "./test.xml",
        serializer.serializeToString(doc),
        function (error) {
            if (error) {
                console.log(error);
            } else {
                console.log("The file was saved!");
            }
        }
    );


});


// 节点的类型。
// const unsigned short      ELEMENT_NODE                   = 1;
// const unsigned short      ATTRIBUTE_NODE                 = 2;
// const unsigned short      TEXT_NODE                      = 3;
// const unsigned short      CDATA_SECTION_NODE             = 4;
// const unsigned short      ENTITY_REFERENCE_NODE          = 5;
// const unsigned short      ENTITY_NODE                    = 6;
// const unsigned short      PROCESSING_INSTRUCTION_NODE    = 7;
// const unsigned short      COMMENT_NODE                   = 8;
// const unsigned short      DOCUMENT_NODE                  = 9;
// const unsigned short      DOCUMENT_TYPE_NODE             = 10;
// const unsigned short      DOCUMENT_FRAGMENT_NODE         = 11;
// const unsigned short      NOTATION_NODE                  = 12;



